﻿window.netbooksApp = window.netbooksApp || {};

window.netbooksApp.datacontext = (function() {
    var datacontext = {
        getBookList: getBookList,
        createBook: createBook,
        saveBook: saveBook,
        saveBookList: saveBookList,
        saveChangedBook: saveChangedBook,
        saveChangedBookList: saveChangedBookList
    };

    return datacontext;

    function getBookList(bookListObservable, errorObservable) {
        return ajaxRequest("get", bookListUrl())
            .done(getSucceeded)
            .fail(getFailed);
        
        function getSucceeded(data) {
            var mappedBookLists = $.map(data, function(list) { return new createBookList(list); });
            bookListObservable(mappedBookLists);
        }
        
        function getFailed(data)
        {
            errorObservable("Error retrieving book list(s).");
        }
    }
    
    function createBookList(data) {
        return new datacontext.bookList(data);
    }

    function createBook(data) {
        return new datacontext.book(data);
    }   

    function saveBook(book) {
        clearErrorMessage(book);
        return ajaxRequest("post", bookUrl(), book)
            .done(postSucceeded)
            .fail(postFailed);
        
        function postSucceeded(data) {
            book.bookId = data.bookId;
        }
        
        function postFailed(data) {
            book.errorMessage("Error adding book to list.");
        }
    }

    function saveBookList(booklist) {
        clearErrorMessage(booklist);
        return ajaxRequest("post", bookListUrl(), booklist)
            .done(postSucceeded)
            .fail(postFailed);
        
        function postSucceeded(data) {
            booklist.bookListId = data.bookListId;
        }
        
        function postFailed(data) {
            booklist.errorMessage("Error adding new book list.");
        }
    }

    function saveChangedBook(book) {
        clearErrorMessage(book);
        return ajaxRequest("put", bookUrl(book.bookId), book, "text")
            .fail(putFailed);
        
        function putFailed() {
            book.errorMessage("Error saving book changes.");
        }
    }

    function saveChangedBookList(booklist) {
        clearErrorMessage(booklist);
        return ajaxRequest("put", bookListUrl(booklist.bookListId), booklist, "text")
            .fail(putFailed);
        
        function putFailed() {
            booklist.errorMessage("Error saving book list.");
        }
    }
    
    //Private

    function clearErrorMessage(entity) { entity.errorMessage(null); }
    
    function ajaxRequest(type, url, data, dataType) {
        var options = {
            dataType: dataType || "json",
            contentType: "application/json",
            cache: false,
            type: type,
            data: data ? data.toJson() : null
        };
        var antiForgeryToken = $("#antiForgeryToken").val();
        if (antiForgeryToken) {
            options.headers = {
                'RequestVerificationToken': antiForgeryToken
            };
        }
        return $.ajax(url, options);
    }
    
    function bookUrl(id) { return "/api/book/" + (id || ""); }
    function bookListUrl(id) { return "/api/booklist/" + (id || ""); }

})();